package com.aijk.commons.utils;

import com.aijk.base.entity.PushAppEnums;
import com.aijk.base.entity.PushRequest;
import com.aijk.commons.exception.ErrorCodeException;
import com.aijk.commons.mq.entity.MqTopicConst;
import com.alibaba.rocketmq.client.producer.SendResult;
import com.aijk.commons.exception.ErrorCodeConst;
import com.aijk.commons.mq.utils.MqUtils;

/**
 * APP 推送工具类
 * User: chendw
 * Date: 2016/7/20 12:51
 * To change this template use File | Settings | File Templates.
 */
public class PushUtils {

    public static SendResult send(PushRequest request) {
        CheckUtils.check(request.getAppcode(), "appcode");
        if (request.getAppcode() == PushAppEnums.resident && request.getEcn() == null) {
            throw new ErrorCodeException(ErrorCodeConst.GLOBAL_REQUEST_ERROR, "push居民端时ecn不能为空");
        }

        if ((request.getAppcode() == PushAppEnums.family_doctor || request.getAppcode() == PushAppEnums.major_doctor) && request.getDid() == null) {
            throw new ErrorCodeException(ErrorCodeConst.GLOBAL_REQUEST_ERROR, "push家医或者会诊医生端时did不能为空");
        }

        CheckUtils.check(request.getCategory(), "category");

        return MqUtils.sendMessage(MqTopicConst.GROUP_PUSH, MqTopicConst.TOPIC_PUSH, "*", String.valueOf(System.currentTimeMillis()), request);

    }
}
